package LinkedList;

import java.util.LinkedList;
import java.util.List;

public class SingleList {
    static class ListNode {
        public int value;//节点的值
        public ListNode next;//下一个节点的地址

        public ListNode(int value) {
            this.value = value;
        }
    }

    public ListNode head;//表示当前链表的头节点

    public void createList() {//创建链表
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(25);
        ListNode node3 = new ListNode(88);
        ListNode node4 = new ListNode(47);
        ListNode node5 = new ListNode(32);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;

    }

    public void display() {//打印链表
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //得到单链表的长度
    public int size() {
        int size = 0;
        ListNode cur = head;
        while(cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }

    //查找是否包含关键字key是否在单链表中
    public boolean contains(int key) {
        boolean ret = false;
        ListNode cur = head;
        while(cur != null) {
            if(cur.value == key) {
                ret = true;
            }
            cur = cur.next;
        }
        return ret;
    }

    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);

        node.next = head;
        head = node;
    }
    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);

        ListNode cur = head;
        if(cur == null) {//如果链表一个节点都没有
            head = node;
            return;
        }
        while(cur.next != null) {//cur.next如果是null就是最后一个节点
            cur = cur.next;
        }//如果不是最后一个节点就++
        cur.next = node;
    }

    //在I位置插入一个元素
    public void insert(int index, int data) {
        ListNode node = new ListNode(data);
        if(index < 0 || index > size()) {
            System.out.println("index不合法");
            return;
        }
        if(index == 0) {
            addFirst(data);
        }
        if(index == size()) {
            addLast(data);
        }
        ListNode cur = findIndexSubOne(index);
        node.next = cur.next;
        cur.next = node;
    }

    private ListNode findIndexSubOne(int index) {
        ListNode cur = head;
        while(index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //删除值为key的节点
    public void delete(int key) {
        if(head != null && head.value == key) {
            head = head.next;
        }
        if (head == null) return;
        ListNode cur = head;
        while(cur.next != null) {
            if(cur.next.value == key) {
                ListNode del = cur.next;
                cur.next = del.next;
            }
            cur = cur.next;
        }
    }

    //清空整个链表
    public void clear() {
        head = null;
    }

    //删除所有值为key的节点
    public void deleteAll(int key) {

        if(head == null) {
            return;
        }
        ListNode cur = head.next;
        ListNode prev = head;
        while(cur != null) {
            if(cur.value == key) {
                prev.next = cur.next;
                cur = cur.next;//继续往下走
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        if(head.value == key) {
            head = head.next;
        }
    }

    //反转链表
    public void reverse(SingleList list) {
        //使用头插法
        if(head == null) return;
        ListNode cur = head.next;
        head.next = null;//把头节点作为最后一个节点，之后的节点使用头插法插入
        //注意先使用一个变量存储cur的下一个节点，不然之后cur发生改变会造成整个链表的丢失

        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
    }

    public void display(ListNode newHead) {//从指定位置打印链表,与之前方法构成了方法的重载
        ListNode cur = newHead;
        while(cur != null) {
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //输出倒数第k个节点
    //使用快慢指针法，快指针先走k-1步，慢指针和快指针同频走，当快指针走到链表最后的时候，慢指针刚好在倒数第k个位置上；
    public int findNthFromEnd(int k) {
        ListNode fast = head;
        ListNode slow = head;
        for (int i = 0; i < k - 1; i++) {
            if(fast.next == null)
                return 0;
        }
        while(fast != null && fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow.value;
    }

    //删除链表的倒数第n个节点
    public void removeNthFromEnd(int n) {
        int key = findNthFromEnd(n);
        if(head != null && head.value == key) {
            head = head.next;
        }
        if(head == null)return;
        ListNode cur = head;
        while(cur != null && cur.next != null) {
            if(cur.next.value == key) {
                ListNode del = cur.next;
                cur.next = del.next;
            }
            cur = cur.next;
        }
    }

    public ListNode queueByk(int k) {//根据k的值把链表分成两部分，大的放在后面，小的放在前面
        ListNode as = null;
        ListNode ae = null;
        ListNode bs = null;
        ListNode be = null;
        ListNode cur = head;
        while(cur != null) {
            if(cur.value < k) {
                //插入数据
                if(bs == null) {
                    bs = be = cur;
                } else {
                    be.next = cur;
                    be = be.next;
                }
            } else {
                if(as ==null) {
                    as = ae =cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if(bs == null) {
            return as;
        }
        be.next = as;
        if(ae != null) {
            ae.next = null;
        }
        return bs;
    }

    //回文链表
    public boolean isPalindrome() {
        ListNode fast = head;
        ListNode slow = head;
        while(fast !=null && fast.next != null) {//先找到中间节点，就是最后的slow
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode cur = slow.next;
        ListNode curNext;//后半部分反转链表
        while(cur != null) {
            curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        while(head != slow) {//head往后走，slow往前走
            if(head.value != slow.value){
                return false;
            }
            if(head.next == slow) {//偶数的时候
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    //相交链表
    public class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            if(headA == null || headB == null) return null;
            //1.先求两个链表的长度
            int lenA = 0;
            int lenB = 0;
            ListNode cur = headA;
            while(cur != null) {
                lenA++;
                cur = cur.next;
            }
            cur = headB;
            while(cur != null) {
                lenB++;
                cur = cur.next;
            }
            ListNode L = headA;//假定L指向的链表是长的，S指向地链表是短的
            ListNode S = headB;
            int len = lenA - lenB;

            if(len < 0) {//len为负数说明S是更长的链表
                L = headB;
                S = headA;//这样保证L指向的链表就是更长的链表
                len = lenB - lenA;//同时保证len一定是正数
            }

            for(int i = 0; L != null && i < len; i++) {
                L = L.next;//让长的链表先走差值步
            }

            while(L != null && S != null) {//同时走
                if(L == S)
                    return L;
                S = S.next;
                L = L.next;
            }
            return null;
        }
    }

    //判断是不是循环链表，使用快慢指针，遍历链表，如果相遇了就说明是循环链表
    public boolean isCircle() {
        if(head == null) return false;
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && slow != null && fast.next != null) {
            if(slow == fast)
                return true;
            slow = slow.next;
            fast = fast.next.next;
        }
        return false;
    }

    //给定一个链表，返回链表开始入环的第一个节点，无环返回空
    public ListNode detectCycle(ListNode head) {
        if(head == null) return null;
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast)//相遇了
                break;
            }
        if(fast == null || fast.next == null) return null;
        fast = head;
        while(fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }

    

}

